package chat.server.controller;

import chat.server.service.ServerService;
import chat.server.view.ServerView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 服务端事件控制类
 *
 * @author 谈笑、
 * @dateTime 2020/10/21 22:52
 */
public class ServerController {

	// 服务端窗体对象
	private ServerView serverView;

	// 服务端功能实现类
	private ServerService serverService;

	public ServerController() {}

	// 构造方法初始化成员属性
	public ServerController(ServerView serverView, ServerService serverService) {
		this.serverView = serverView;
		this.serverService = serverService;
	}

	/**
	 * 功能操作按钮点击事件
	 */
	public void serverButtonEvent() {
		// 启动服务
		this.serverView.getjButtons()[0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				serverService.startServer(serverView);
			}
		});

		// 停止服务
		this.serverView.getjButtons()[1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				serverService.stopServer(serverView);
			}
		});

		// 退出程序
		this.serverView.getjButtons()[2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				serverService.serverSignOut(serverView);
			}
		});
	}

	/**
	 * 窗体关闭事件
	 */
	public void closeServerWindow() {
		this.serverView.getjFrame().addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				serverService.serverSignOut(serverView);
			}
		});
	}

}